package zoolisten;

import obth.Tool;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.Scanner;

public class Trainer {

    public static void debugEX(Object o){
        System.out.println(Thread.currentThread().getName()+
                " : "+System.currentTimeMillis()+" --- "+o.getClass().getName()+" === "+o);
    }

    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = new ZooKeeper(
                    "master:2181",
                    3600,new DWatcher());
            new Thread(new ControllRunner(zooKeeper),"cli").start();

        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
class DWatcher implements Watcher{

    @Override
    public void process(WatchedEvent event) {
        Trainer.debugEX(event);
    }
}
class ControllRunner implements Runnable{


    private ZooKeeper zooKeeper;

    public ControllRunner(ZooKeeper zooKeeper) {

        this.zooKeeper = zooKeeper;
    }

    @Override
    public void run() {

        Scanner scanner = new Scanner(System.in);
        Stat stat;
        String s;
        while (true){
            s = scanner.nextLine();
            try {
                stat = zooKeeper.setData("/some", s.getBytes(), -1);
                Trainer.debugEX(stat);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}